import {
    getCUserName
} from '../../api/app'
import {
    getJobDetail,
    applyJob,
    getJobApplyDate
} from '../../api/job'
import {
    openSalaryCard
} from '../../utils/salary-card'
import user from '../../utils/user'
const app = getApp()
Page({
    data: {
        job: {},
        distance: 1000,
        isShowReport: false,
        inviteName: '',
        showSalary: false,
        disableList: '',
        jobId: '',
        shareJobId: '',
        hideButton: false
    },
    static: {
        subMchid: '',
        scene: '',
    },
    async onLoad(options) {
        console.log(options, 'options')
        this.static.scene = options.scene

        this.setData({
            jobId: options.id,
            hideButton: options?.from === 'signUp'
        })
        if (options.scene) {
            let scene = decodeURIComponent(options.scene)
            const option = {}
            scene = scene.split('&')
            console.log(scene, 'scenescenescenescene')
            for (const i in scene) {
                const query = scene[i].split('=')
                option[query[0]] = query[1]
            }
            console.log(option, 'optionoptionoption')
            options = option
        }
        console.log(options, '转换后的')
        if (options.uid) {
            this.setData({
                shareJobId: options.id
            })
        }
        user.waitLogin().then(async res => {
            await this.onGetJobDetail()
            await this.currentLocation()
        }).catch(async rej => {
            await this.onGetJobDetail()
            await this.currentLocation()
        })

    },
    onPreview(event) {
        const {
            i
        } = event.currentTarget.dataset
        const imageList = []
        this.data.job.params.jobFile.forEach(ite => {
            if (ite.fileType === 'video') {
                imageList.push({
                    url: ite.tempFilePath || ite.fileUrl,
                    type: 'video'
                })
            } else {
                imageList.push({
                    url: ite.tempFilePath || ite.fileUrl,
                    type: 'image'
                })
            }
        })
        wx.previewMedia({
            sources: imageList,
            current: i
        })
    },
    async onGetJobDetail() {
        const job = await getJobDetail({
            // enterType 0=C端过程 2b端用户分享 5日日保进入 6 长期保 进入
            jobId: this.data.jobId || this.data.shareJobId,
            enterType: this.data.jobId ? 0 : 2
        })
        console.log(job, 'iob')
        job.jobTag = job.jobTag ? job.jobTag.split(';') : []
        job.jobLocation = job.jobLocation.split(',')
        job.longitude = job.jobLocation[0]
        job.latitude = job.jobLocation[1]
        job.jobDetail = job.jobDetail.replace(/[\r\n]/g, '「').split('「').filter(item => item)
        job.jobDetailRich = job.jobDetail.map(item => {
            return item.replace(/#[1-9|\u4e00-\u9fa5|A-Z|a-z]{1,}/g, function (word) {
                return '<span style="color:#3E5FF5">' + word + '</span>'
            })
        })
        this.setData({
            job
        })

    },
    // 点击报名
    async onApplyJob() {
        if (!user.userId) {
            await app.confirm('您还未登录，是否立即去登录？')
            user.goRegister(true, '/pages/job-detail/job-detail?scene=' + this.static.scene)
            return
        }
        if (this.data.job.params.workerApplyDetail != undefined) {
            wx.navigateTo({
                url: '/pages/job-status/job-status?jobid=' + this.data.job.id + '&type=' + 'success' + '&title=' + '您已报名',
                events: {},
                // success(res){
                //     res.eventChannel.emit('reportDate', { data: this.data.job })
                // }
            })
            return
        }
        console.log(this.data.jobId, 'jobId')
        const result = await applyJob({
            jobId: this.data.job.id,
            applyType: this.data.jobId ? 0 : 2
        })
        setTimeout(() => {
            wx.navigateTo({
                url: '/pages/job-status/job-status?jobid=' + this.data.job.id + '&type=' + 'success' + '&title=' + '报名成功',
                events: {},
            })
        }, 500)

    },
    async reportDateSelected(event) {
        if (!event.detail.length) {
            app.toast('请选择报名日期')
            return
        }
        this.setData({
            isShowReport: false,
            disableList: ''
        })

    },
    reportDateCancel() {
        this.setData({
            isShowReport: false
        })
    },
    addressNav() {
        wx.openLocation({
            latitude: Number(this.data.job.latitude), // 维度
            longitude: Number(this.data.job.longitude), // 经度
            name: this.data.job.jobAddress, // 目的地定位名称
            scale: 15, // 缩放比例
            address: this.data.job.jobAddress // 导航详细地址
        })
    },
    async currentLocation() {

        const result = await wx.getLocation({
            type: 'gcj02'
        })
        console.log(result, '当前位置经纬度')
        console.log(this.data.job, '项目位置经纬度')
        // this.longitude = res.longitude
        // this.latitude = res.latitude
        // 计算当前位置和公司位置，之间的距离
        var La1 = (result.latitude * Math.PI) / 180.0
        var La2 = (this.data.job.latitude * Math.PI) / 180.0
        var La3 = La1 - La2
        var Lb3 =
            (result.longitude * Math.PI) / 180.0 -
            (this.data.job.longitude * Math.PI) /
            180.0
        var s =
            2 *
            Math.asin(
                Math.sqrt(
                    Math.pow(Math.sin(La3 / 2), 2) +
                    Math.cos(La1) *
                    Math.cos(La2) *
                    Math.pow(Math.sin(Lb3 / 2), 2)
                )
            )
        s = s * 6378.137 // 地球半径
        s = Math.round(s * 10000) / 10000
        this.setData({
            distance: parseInt(s)
        })
    },
    onHideSalary() {
        this.setData({
            showSalary: false,
            isShowReport: true
        })
    },
    async onOpenSalary() {
        const result = await openSalaryCard(this.data.job.companyId, user.userName, user.cardNo)
        this.static.subMchid = result.subMchid

    }

})